package com.zjsru.oneDay202210;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: CookLee
 * @Date: 2022/10/15
 * 用栈操作构建数组
 *
 * "Push"：从 list 中读取一个新元素， 并将其推入数组中。
 * "Pop"：删除数组中的最后一个元素。
 *
 * 输入：target = [1,3], n = 3
 * 输出：["Push","Push","Pop","Push"]
 * 解释：
 * 读取 1 并自动推入数组 -> [1]
 * 读取 2 并自动推入数组，然后删除它 -> [1]
 * 读取 3 并自动推入数组 -> [1,3]
 */
public class BuildArray {
    
    private static String PUSH = "Push";
    
    private static String POP = "Pop";
    
    
    /**
     * 模拟
     */
    public List<String> buildArray(int[] target, int n) {
        List<String> ansList = new ArrayList<>();
        int prev = 0;
        for (int num : target) {
            //在 target 中每两个连续的数字 prev 和 number 中插入 number−prev−1个 Push 和 Pop
            for (int i = 0; i < num - prev - 1; i++) {
                ansList.add(PUSH);
                ansList.add(POP);
            }
            //加一个 Push 来插入当前数字即可。
            ansList.add(PUSH);
            prev = num;
        }
        return ansList;
    }
    
    public static void main(String[] args) {
        BuildArray buildArray = new BuildArray();
        int[] target = new int[] {1, 3};
        int n = 3;
        System.out.println(buildArray.buildArray(target, n));
    }
}
